<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------


namespace User\Controller;
use Common\Api\UserApi;
use Think\Controller;
use OT\File;
use Common\Tools;

/**
 * 用户控制器
 * 包括用户中心，用户登录及注册
 */

function utf8togb($s) { 
    return iconv('utf-8', 'GB2312//IGNORE', $s); // IGNORE 参数是遇到不成转换的字符时忽略 
} 
function gbtoutf8($s) { 
    return iconv('GB2312', 'utf-8//IGNORE', $s); // IGNORE 参数是遇到不成转换的字符时忽略 
} 


class MembersController extends \Common\Controller\FrontController {


	public function _initialize() {
		parent::_initialize();
        //访问者控制
        if(!$this->visitor->is_login) {
            if(in_array(ACTION_NAME, array('index','pms','sign_in'))){
                IS_AJAX && $this->ajaxReturn(0, L('login_please'),'',1);
                //非ajax的跳转页面
                $this->redirect('members/login');
            }
        }else{
            $urls = array('1'=>'company/index','2'=>'personal/index');
            !IS_AJAX && !in_array(ACTION_NAME, array('logout','varify_email')) && $this->redirect($urls[C('visitor.utype')]);
        }
	}
	public function index(){}
    /**
     * [login 用户登录]
     */
	public function login() {
        if(!I('get.org','','trim') && C('PLATFORM') == 'mobile' && $this->apply['Mobile']){
            redirect(build_mobile_url(array('c'=>'Members','a'=>'login')));
        }
        if(IS_AJAX && IS_POST){
            $expire = I('post.expire',0,'intval');
            $index_login = I('post.index_login',0,'intval');
            $url = I('post.url','','trim');
            if (C('qscms_captcha_open')==1 && (C('qscms_captcha_config.user_login')==0 || (session('?error_login_count') && session('error_login_count')>=C('qscms_captcha_config.user_login')))){
                if(true !== $reg = \Common\qscmslib\captcha::verify()) $this->ajaxReturn(0,$reg);
            }
            $passport = $this->_user_server();
            if($mobile = I('post.mobile','','trim')){
                if(!fieldRegex($mobile,'mobile')) $this->ajaxReturn(0,'手机号格式错误！');
                $smsVerify = session('login_smsVerify');
                !$smsVerify && $this->ajaxReturn(0,'验证码错误！');//验证码错误！
                if($mobile != $smsVerify['mobile']) $this->ajaxReturn(0,'手机号不一致！');//手机号不一致
                if(time()>$smsVerify['time']+600) $this->ajaxReturn(0,'验证码过期！');//验证码过期
                $vcode_sms = I('post.mobile_vcode',0,'intval');
                $mobile_rand=substr(md5($vcode_sms), 8,16);
                if($mobile_rand!=$smsVerify['rand']) $this->ajaxReturn(0,'验证码错误！');//验证码错误！
                $user = M('Members')->where(array('mobile'=>$smsVerify['mobile']))->find();
                var_dump($user);die;
                !$user && $err = '帐号不存在！';
                $uid = $user['uid'];
                if(!$user['mobile_audit']){
                    $setsqlarr['mobile'] = $smsVerify['mobile'];
                    $setsqlarr['mobile_audit']=1;
                    if(false !== $reg = M('Members')->where(array('uid'=>$uid))->save($setsqlarr)){
                        D('Members')->update_user_info($setsqlarr,$user);
                        if($user['utype']=='1'){
                            $rule=D('Task')->get_task_cache($user['utype'],22);
                            D('TaskLog')->do_task($user,22);
                        }else{
                            $rule=D('Task')->get_task_cache($user['utype'],7);
                            D('TaskLog')->do_task($user,7);
                        }
                        write_members_log($user,8002);
                    }
                }
                session('login_smsVerify',null);
            }else{
                $username = I('post.username','','trim');
                $password = I('post.password','','trim');
                if(false === $uid = $passport->uc('default')->auth($username, $password)){
                    $err = $passport->get_error();
                    if($err == L('auth_null') && false !== $passport->uc('ucenter')->auth($username, $password)){
                        cookie('members_uc_info', $passport->_user);
                        $this->ajaxReturn(1,'',U('members/apilogin_ucenter'));
                    }
                }elseif(C('apply.Ucenter') && C('qscms_uc_open')){
                    $user = $passport->_user;
                    if(!$user['uc_uid']) $passport->uc('ucenter')->register($user);
                }
            }
            if($uid){
                if(false === $this->visitor->login($uid, $expire)) $this->ajaxReturn(0,$this->visitor->getError());
                $urls = array('1'=>'company/index','2'=>'personal/index');
                $login_url = $url ? $url : U($urls[$this->visitor->info['utype']]);
                //同步登陆
                $passport->uc('ucenter')->synlogin($uid);
                $this->ajaxReturn(1,'登录成功！',$login_url);
            }
            //记录登录错误次数
            if(C('qscms_captcha_open')==1){
                if(C('qscms_captcha_config.user_login')>0){
                    $error_login_count = session('?error_login_count')?(session('error_login_count')+1):1;
                    session('error_login_count',$error_login_count);
                    if(session('error_login_count')>=C('qscms_captcha_config.user_login')){
                        $verify_userlogin = 1;
                    }else{
                        $verify_userlogin = 0;
                    }
                }else{
                    $verify_userlogin = 1;
                }
            }else{
                $verify_userlogin = 0;
            }
            
            $this->ajaxReturn(0,$err,$verify_userlogin);
        }else{
            if($this->visitor->is_login){
                $urls = array('1'=>'company/index','2'=>'personal/index');
                $this->redirect($urls[C('visitor.utype')]);
            }
            if(false === $oauth_list = F('oauth_list')){
                $oauth_list = D('Oauth')->oauth_cache();
            }
            $this->assign('oauth_list',$oauth_list);
            $this->assign('title','会员登录 - '.C('qscms_site_name'));
            $this->assign('weixin_login',C('qscms_weixin_apiopen') && C('qscms_weixin_scan_login'));//微信扫描登录是否开启
            $this->assign('verify_userlogin',$this->check_captcha_open(C('qscms_captcha_config.user_login'),'error_login_count'));
            $this->display();
        }
    }



    public function drsj(){
        ini_set("memory_limit","-1");
        set_time_limit(0);
        header("Content-Type: text/html;charset=gbk");
   
        //import("ORG.File");
        //$tbs = array('校理','校文','专理','专文');
        $tbs = array('sql');
        $filepath=realpath(APP_PATH."sql.accdb");

        $sql_filename=APP_PATH."ani_modular.sql";;

        $conn = new \COM('ADODB.Connection') or die('can not start Active X Data Objects');

        $connstr="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$filepath";

        $conn->Open($connstr);

        $SqlStr="select * from news where news_sort=11401";

        //echo utf8togb($SqlStr);
        $rs = $conn->Execute($SqlStr);

        while(!$rs->EOF){

            $gbinf['news_sort']=1155;     
            $gbinf['title']=gbtoutf8($rs->Fields['title']->Value)!=''?"'".gbtoutf8($rs->Fields['title']->Value)."'":"NULL";
             $gbinf['UpTime']=strtotime(gbtoutf8($rs->Fields['UpTime']->Value)!=''?gbtoutf8($rs->Fields['UpTime']->Value):"NULL");
            $gbinf['uid']=1;
            $gbinf['group_id']=0;
            $gbinf['model_id']=3;
            $gbinf['display']=1;
            $gbinf['status']=1;
            $gbinf['create_time']=strtotime(gbtoutf8($rs->Fields['UpTime']->Value)!=''?gbtoutf8($rs->Fields['UpTime']->Value):"NULL");     
     // $gbinf['content']=gbtoutf8($rs->Fields['content']->Value)!=''?"'".gbtoutf8($rs->Fields['content']->Value)."'":"NULL";
            $str=implode(',', $gbinf);
          if(strpos($str,",,")>0){
                $rs->MoveNext();
                continue;
            }

            if($str){
                $s[]="(". $str .")";
            }
            if($s){           
                $sql.="INSERT INTO `sn_document` (`category_id`,`title`,`update_time`,`uid`,`group_id`,`model_id`,`display`,`status`,`create_time`) VALUES " . implode(',', $s) . ";\r\n";
                 $sql.="INSERT INTO `sn_document_article` (`id`,`content`,`parse`,`bookmark`,`publish_state`,`newstype`,`indexpic`,`detailsimg`) VALUES ((SELECT MAX(id) FROM sn_document),'".str_replace("'","''",(gbtoutf8($rs->Fields['content']->Value)!=''?gbtoutf8($rs->Fields['content']->Value):NULL))."',0,0,0,'公告',0,0);\r\n" ;
                $s=array();
            }
            $rs->MoveNext();
        }
        $rs->Close();
        File::write_file($sql_filename,$sql,"a");
    }





public function drhy(){

        ini_set("memory_limit","-1");
        set_time_limit(0);
        header("Content-Type: text/html;charset=gbk");
   
        //import("ORG.File");
        //$tbs = array('校理','校文','专理','专文');
        $tbs = array('sql');
        $filepath=realpath(APP_PATH."sql.accdb");

        $sql_filename=APP_PATH."ani_modular.sql";;

        $conn = new \COM('ADODB.Connection') or die('can not start Active X Data Objects');

        $connstr="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$filepath";

        $conn->Open($connstr);

        $SqlStr="select * from member";
   
        //echo utf8togb($SqlStr);
        $rs = $conn->Execute($SqlStr);

        while(!$rs->EOF){

            $gbinf['U_Name']=gbtoutf8($rs->Fields['U_Name']->Value)!=''?gbtoutf8($rs->Fields['U_Name']->Value):"NULL";   
            $gbinf['U_Password']="b8cf4a0a0d5952a3aa0fde08003b2abc"; 

            $gbinf['U_TrueName']=gbtoutf8($rs->Fields['U_TrueName']->Value)!=''?gbtoutf8($rs->Fields['U_TrueName']->Value):"NULL"; 



            $gbinf['U_TrueName']=gbtoutf8($rs->Fields['U_TrueName']->Value)!=''?gbtoutf8($rs->Fields['U_TrueName']->Value):"NULL"; 
               
            $gbinf['U_Email']=gbtoutf8($rs->Fields['U_Email']->Value)!=''?gbtoutf8($rs->Fields['U_Email']->Value):"NULL";    
            $gbinf['U_Tel']=gbtoutf8($rs->Fields['U_Tel']->Value)!=''?gbtoutf8($rs->Fields['U_Tel']->Value):"NULL";    
            $gbinf['U_Address']=gbtoutf8($rs->Fields['U_Address']->Value)!=''?gbtoutf8($rs->Fields['U_Address']->Value):"NULL"; 
            $gbinf['U_Postcode']=gbtoutf8($rs->Fields['U_Postcode']->Value)!=''?gbtoutf8($rs->Fields['U_Postcode']->Value):"NULL";
            $gbinf['U_QQ']=gbtoutf8($rs->Fields['U_QQ']->Value)!=''?gbtoutf8($rs->Fields['U_QQ']->Value):"NULL";
            $gbinf['R_icode']=gbtoutf8($rs->Fields['R_icode']->Value)!=''?gbtoutf8($rs->Fields['R_icode']->Value):"NULL";
            $gbinf['U_britherday']=gbtoutf8($rs->Fields['U_britherday']->Value)!=''?gbtoutf8($rs->Fields['U_britherday']->Value):"NULL";  
            $gbinf['R_phone']=gbtoutf8($rs->Fields['R_phone']->Value)!=''?gbtoutf8($rs->Fields['R_phone']->Value):"NULL";   
            $gbinf['U_Sex']=gbtoutf8($rs->Fields['U_Sex']->Value)!=''?gbtoutf8($rs->Fields['U_Sex']->Value):"NULL";
            $gbinf['RegTime']=gbtoutf8($rs->Fields['RegTime']->Value)!=''?gbtoutf8($rs->Fields['RegTime']->Value):"NULL";   
            $gbinf['RegIP']=gbtoutf8($rs->Fields['RegIP']->Value)!=''?gbtoutf8($rs->Fields['RegIP']->Value):"NULL";    
            $gbinf['R_qm']=gbtoutf8($rs->Fields['R_qm']->Value)!=''?gbtoutf8($rs->Fields['R_qm']->Value):"NULL";
            $gbinf['company']=gbtoutf8($rs->Fields['company']->Value)!=''?gbtoutf8($rs->Fields['company']->Value):"NULL";
            $gbinf['qyxz']=gbtoutf8($rs->Fields['qyxz']->Value)!=''?gbtoutf8($rs->Fields['qyxz']->Value):"NULL";    
            $gbinf['szdq']=gbtoutf8($rs->Fields['szdq']->Value)!=''?gbtoutf8($rs->Fields['szdq']->Value):"NULL";
            $gbinf['ywfw']=gbtoutf8($rs->Fields['ywfw']->Value)!=''?gbtoutf8($rs->Fields['ywfw']->Value):"NULL";
            $gbinf['lxdh']=gbtoutf8($rs->Fields['lxdh']->Value)!=''?gbtoutf8($rs->Fields['lxdh']->Value):"NULL";
            $gbinf['gscz']=gbtoutf8($rs->Fields['gscz']->Value)!=''?gbtoutf8($rs->Fields['gscz']->Value):"NULL";
            $gbinf['lxr']=gbtoutf8($rs->Fields['lxr']->Value)!=''?gbtoutf8($rs->Fields['lxr']->Value):"NULL";
            $gbinf['qywz']=gbtoutf8($rs->Fields['qywz']->Value)!=''?gbtoutf8($rs->Fields['qywz']->Value):"NULL";
            $gbinf['gsjj']=gbtoutf8($rs->Fields['gsjj']->Value)!=''?gbtoutf8($rs->Fields['gsjj']->Value):"NULL";
            $gbinf['yjdz']=gbtoutf8($rs->Fields['yjdz']->Value)!=''?gbtoutf8($rs->Fields['yjdz']->Value):"NULL";
            $gbinf['yjdz']=gbtoutf8($rs->Fields['yjdz']->Value)!=''?gbtoutf8($rs->Fields['yjdz']->Value):"NULL";
            $gbinf['yzbm']=gbtoutf8($rs->Fields['yzbm']->Value)!=''?gbtoutf8($rs->Fields['yzbm']->Value):"NULL";
      


     // $gbinf['content']=gbtoutf8($rs->Fields['content']->Value)!=''?"'".gbtoutf8($rs->Fields['content']->Value)."'":"NULL";
            $str=implode(',', $gbinf);
          if(strpos($str,",,")>0){
                $rs->MoveNext();
                continue;
            }

            if($str){
                $s[]="(". $str .")";
            }
            if($s){           
  $sql.="INSERT INTO `sn_member` (`username`,`password`,`nickname`,`email`,`mobile`,`sex`,`birthday`,`qq`,`score`,`signature`,`pos_province`,`postcode`,`salt`,`login`,`reg_ip`,`reg_time`,`last_login_ip`,`last_login_time`,`status`,`type`,`department`,`position`,`motel`,`truename`,`companyname`,`nature`,`location`,`business`,`number`,`fax`,`contact`,`url`,`profile`,`mail`,`code`) VALUES " . implode(',', $s) . ";\r\n";
              
                $s=array();
            }
            $rs->MoveNext();
        }
        $rs->Close();
        File::write_file($sql_filename,$sql,"a");
    }






    /**
     * 用户退出
     */
    public function logout() {
		$this->visitor->logout();
		//同步退出
		$passport = $this->_user_server();
		$synlogout = $passport->synlogout();
        $this->redirect('members/login');
    }
    /**
     * [register 会员注册]
     */
    public function register(){
        if(!I('get.org','','trim') && C('PLATFORM') == 'mobile' && $this->apply['Mobile']){
            redirect(build_mobile_url(array('c'=>'Members','a'=>'register')));
        }
        if (C('qscms_closereg')){
            IS_AJAX && $this->ajaxReturn(0,'网站暂停会员注册，请稍后再次尝试！');
            $this->error("网站暂停会员注册，请稍后再次尝试！");
        }
        if(IS_POST && IS_AJAX){
            $data['reg_type'] = I('post.reg_type',0,'intval');//注册方式(1:手机，2:邮箱，3:微信)
            $array = array(1 => 'mobile',2 => 'email');
            if(!$reg = $array[$data['reg_type']]) $this->ajaxReturn(0,'正确选择注册方式！');
            $data['utype'] = I('post.utype',0,'intval');
            if($data['utype'] != 1 && $data['utype'] != 2) $this->ajaxReturn(0,'请正确选择会员类型!');
            if($data['reg_type'] == 1){
                $data['mobile'] = I('post.mobile',0,'trim');
                $smsVerify = session('reg_smsVerify');
                if(!$smsVerify) $this->ajaxReturn(0,'验证码错误！');
                if($data['mobile'] != $smsVerify['mobile']) $this->ajaxReturn(0,'手机号不一致！',$smsVerify);//手机号不一致
                if(time()>$smsVerify['time']+600) $this->ajaxReturn(0,'验证码过期！');//验证码过期
                $vcode_sms = I('post.mobile_vcode',0,'intval');
                $mobile_rand=substr(md5($vcode_sms), 8,16);
                if($mobile_rand!=$smsVerify['rand']) $this->ajaxReturn(0,'验证码错误！');//验证码错误！
                $data['password'] = I('post.password','','trim');
                $passwordVerify = I('post.passwordVerify','','trim');
            }elseif('bind' == $ucenter = I('post.ucenter','','trim')){
                $uc_user = cookie('members_uc_info');
                $data = array_merge($data,$uc_user);
                $passwordVerify = $data['password'];
                $data['utype']==1 && $data['mobile'] = I('post.telephone','','trim,badword');
                C('qscms_check_reg_email') && $data['status'] = 0;
            }else{
                if($data['utype'] == 1){
                    $data['password'] = I('post.cpassword','','trim');
                    $passwordVerify = I('post.cpasswordVerify','','trim');
                }else{
                    $data['password'] = I('post.emailpassword','','trim');
                    $passwordVerify = I('post.emailpasswordVerify','','trim');
                }
                $data['username'] = I('post.username','','trim,badword');
                $data['email'] = I('post.email','','trim,badword');
                $data['utype']==1 && $data['mobile'] = I('post.telephone','','trim,badword');
                C('qscms_check_reg_email') && $data['status'] = 0;
            }

            $data['username'] = I('post.username','','trim,badword');//个人用户注册增加用户名
            !$data['password'] && $this->ajaxReturn(0,'请输入密码!');
            $data['password'] != $passwordVerify && $this->ajaxReturn(0,'两次密码输入不一致!');
            if($data['utype']==1){//如果为企业注册，则判断是否有上传
                if(I('post.certificate_img_up',0) || session('?certificate_img')){
                    $com_setarr['audit'] = 2;
                    if(I('post.certificate_img_up'))
                        $com_setarr['certificate_img']=I('post.certificate_img_up');
                    else
                        $com_setarr['certificate_img']=session('certificate_img');
                }else{

                    $com_setarr['audit'] = 0;
                    $this->ajaxReturn(0,'请上传营业执照!');
                }
                $com_setarr['email']=$data['email'];
                $com_setarr['email_show']=0;
                $com_setarr['companyname']=I('post.companyname','','trim,badword');
                $com_setarr['contact']=I('post.contact','','trim,badword');
                $com_setarr['contact_show']=0;
                $com_setarr['telephone']=I('post.telephone','','trim,badword');
                $com_setarr['telephone_show']=0;
                $com_setarr['landline_tel']=I('post.landline_tel','','trim,badword');
                $com_setarr['landline_tel_show']=0;
                $company_mod = D('CompanyProfile');
                if(false === $company_mod->create($com_setarr)) $this->ajaxReturn(0,$company_mod->getError());
            }
            $us = (!$uc_user && C('apply.Ucenter') && C('qscms_uc_open')) ? 'ucenter' : 'default';
            $passport = $this->_user_server($us);
            if(false === $data = $passport->register($data)){
                if($user = $passport->get_status()) $this->ajaxReturn(1,'会员注册成功！',array('url'=>U('members/reg_email_activate',array('uid'=>$user['uid']))));
                $this->ajaxReturn(0,$passport->get_error());
            }
            // 添加企业信息
            if($data['utype']==1){
                $company_mod->uid=$data['uid'];
                C('SUBSITE_VAL.s_id') && $company_mod->subsite_id = C('SUBSITE_VAL.s_id');
                $insert_company_id = $company_mod->add();
                if($insert_company_id){
                    switch($com_setarr['audit']){
                        case 1:
                            $audit_str = '认证通过';break;
                        case 2:
                            $audit_str = '认证中';break;
                        case 3:
                            $audit_str = '认证未通过';break;
                        default:
                            $audit_str = '';break;
                    }
                    if($audit_str){
                        $auditsqlarr['company_id']=$insert_company_id;
                        $auditsqlarr['reason']='自动设置';
                        $auditsqlarr['status']=$audit_str;
                        $auditsqlarr['addtime']=time();
                        $auditsqlarr['audit_man']='系统';
                        M('AuditReason')->data($auditsqlarr)->add();
                    }
                }
            }
            if('bind' == I('post.org','','trim') && cookie('members_bind_info')){
                $user_bind_info = object_to_array(cookie('members_bind_info'));
                $user_bind_info['uid'] = $data['uid'];
                $oauth = new \Common\qscmslib\oauth($user_bind_info['type']);
                $oauth->bindUser($user_bind_info);
                $this->_save_avatar($user_bind_info['temp_avatar'],$data['uid']);//临时头像转换
                cookie('members_bind_info', NULL);//清理绑定COOKIE
            }
            session('reg_smsVerify',null);
            D('Members')->user_register($data);
            $incode = I('post.incode','','trim');
            if($data['reg_type'] == 2 && C('qscms_check_reg_email')){//是否需要邮箱激活
                $time=time();
                $key=substr(md5($data['email'].$time),8,16);
                $str = encrypt(http_build_query(array('e'=>$data['email'],'k'=>$key,'t'=>$time,'p'=>$data['password'],'n'=>$data['username'],'i'=>$incode)));
                $send_mail['send_type']='set_auth';
                $send_mail['sendto_email']=$data['email'];
                $send_mail['subject']='set_auth_title';
                $send_mail['body']='set_auth';
                $replac_mail['auth_url']=C('qscms_site_domain').U('home/members/activate',array('key'=>$str),true,false,true);
                if(true !== $reg = D('Mailconfig')->send_mail($send_mail,$replac_mail)) $this->ajaxReturn(0,$reg);
                $this->ajaxReturn(1,'会员注册成功！',array('url'=>U('members/reg_email_activate',array('uid'=>$data['uid']))));
            }
            //如果是推荐注册，赠送积分
            $this->_incode($incode);
            $this->_correlation($data);
            $points_rule = D('Task')->get_task_cache(2,1);
            $result['url'] = $data['utype']==2 ? U('personal/resume_add',array('points'=>$points_rule['points'],'first'=>1)) : U('members/index');
            $this->ajaxReturn(1,'会员注册成功！',$result);
        }else{
            $utype = I('get.utype',0,'intval');
            $utype == 0 && $type = 'reg';
            $utype == 1 && $type = 'reg_company';
            $utype == 2 && $type = 'reg_personal';
            //第三方登录
            if(false === $oauth_list = F('oauth_list')){
                $oauth_list = D('Oauth')->oauth_cache();
            }
            $this->assign('utype',$utype);//注册会员类型
            $this->assign('user_bind',$user_bind);
            $this->assign('oauth_list',$oauth_list);
            $this->assign('company_repeat',C('qscms_company_repeat'));//企业注册名称是否可以重复
            $this->_config_seo(array('title'=>'会员注册 - '.C('qscms_site_name')));
            $this->display($type);
        }
    }
    /**
     * [_incode 注册赠送积分]
     */
    protected function _incode($incode){
        if($incode){
            if(preg_match('/^[a-zA-Z0-9]{8}$/',$incode)){  
                $inviter_info = M('Members')->where(array('invitation_code'=>$incode))->find();
                if($inviter_info){
                    $task_id = $inviter_info['utype']==1?31:14;
                    D('TaskLog')->do_task($inviter_info,$task_id);
                }
            }
        }
    }
    /**
     * [_correlation 用户注册相关]
     */
    protected function _correlation($data){
        $members_mod = D('Members');
        if(false === $this->visitor->login($data['uid'])){
            IS_AJAX && $this->ajaxReturn(0,$this->visitor->getError());
            $this->error($this->visitor->getError(),'register');
        }
        if($data['reg_type'] == 2){
            if(false === $mailconfig = F('mailconfig')) $mailconfig = D('Mailconfig')->get_cache();//邮箱系统配置参数
            if($mailconfig['set_reg']==1){
                $utype_cn = array('1'=>'企业','2'=>'个人');
                $send_mail['send_type']='set_reg';
                $send_mail['sendto_email']=$data['email'];
                $send_mail['subject']='set_reg_title';
                $send_mail['body']='set_reg';
                $replac_mail['username']=$this->visitor->info['username'];
                $replac_mail['password']=$data['password'];
                $replac_mail['utype']=$utype_cn[$data['utype']];
                D('Mailconfig')->send_mail($send_mail,$replac_mail);
            }
        }
        //同步登陆
        $this->_user_server()->synlogin($data['uid']);
    }
    /**
     * [activate 邮箱注册激活]
     */
    public function activate(){
        if(!I('get.org','','trim') && C('PLATFORM') == 'mobile' && $this->apply['Mobile']){
            redirect(U('mobile/members/activate',array('key'=>I('get.key','','trim')),true,false,true));
        }
        parse_str(decrypt(I('get.key','','trim')),$data);
        !fieldRegex($data['e'],'email') && $this->error('邮箱格式错误！',U('members/register'));
        $end_time=$data['t']+24*3600;
        if($end_time<time()) $this->error('注册失败,链接过期!',U('members/register'));
        $key_str=substr(md5($data['e'].$data['t']),8,16);
        if($key_str!=$data['k']) $this->error('注册失败,key错误',U('members/register'));
        $members_mod = D('Members');
        $user = $members_mod->field('uid,utype,email,status')->where(array('email'=>$data['e']))->find();
        !$user && $this->error('帐号不存在！',U('members/register'));
        $points_rule = D('Task')->get_task_cache(2,1);
        $urls = array('1'=>'company/index','2'=>U('personal/resume_add',array('points'=>$points_rule['points'],'first'=>1)));
        if($user['status'] == 0){
            $d = array('username'=>$data['n'],'password'=>$data['p'],'status'=>1,'email_audit'=>1);
            $passport = $this->_user_server();
            if(false === $uid = $passport->edit($user['uid'],$d)) $this->error('帐号激活失败，请重新操作！',U('members/register'));
            $user['reg_type'] = 2;
            $user['password'] = $data['p'];
            $data['i'] && $this->_incode($data['i']);
            $this->_correlation($user);
            $this->success('帐号激活成功！',$urls[$this->visitor->info['utype']]);
        }else{
            $this->success('帐号已经激活,请登录！',U('members/login'));
        }
    }
    /**
     * [reg_email_activate description]
     */
    public function reg_email_activate(){
        $uid = I('get.uid',0,'intval');
        !$uid && $this->redirect('members/register');
        $user = M('Members')->field('uid,email')->where(array('uid'=>$uid,'status'=>0))->find();
        !$user && $this->redirect('members/register');
        $this->assign('user',$user);
        $this->assign('title','会员注册 - '.C('qscms_site_name'));
        $this->display();
    }
    /**
     * [send_sms 注册验证短信]
     */
    public function verify_sms(){
        $mobile=I('get.mobile','','trim');
        if(!fieldRegex($mobile,'mobile')) $this->ajaxReturn(0,'手机号格式错误！');
        $smsVerify = session('reg_smsVerify');
        if($mobile!=$smsVerify('mobile')) $this->ajaxReturn(0,'手机号不一致！');//手机号不一致
        if(time()>$smsVerify('time')+600) $this->ajaxReturn(0,'验证码过期！');//验证码过期
        $vcode_sms = I('get.mobile_vcode',0,'intval');
        $mobile_rand=substr(md5($vcode_sms), 8,16);
        if($mobile_rand!=$smsVerify('rand')) $this->ajaxReturn(0,'验证码错误！');//验证码错误！
        $this->ajaxReturn(1,'手机验证成功！');
    }
    // 注册发送短信/找回密码 短信
    public function reg_send_sms(){
        if(C('qscms_captcha_open') && C('qscms_captcha_config.varify_mobile') && true !== $reg = \Common\qscmslib\captcha::verify()) $this->ajaxReturn(0,$reg);
        if($uid = I('post.uid',0,'intval')){
            $mobile=M('Members')->where(array('uid'=>$uid))->getfield('mobile');
            !$mobile && $this->ajaxReturn(0,'用户不存在！');
        }else{
            $mobile = I('post.mobile','','trim');
            !$mobile && $this->ajaxReturn(0,'请填手机号码！');
        }
        if(!fieldRegex($mobile,'mobile')) $this->ajaxReturn(0,'手机号错误！');
        $sms_type = I('post.sms_type','reg','trim');
        $rand=mt_rand(100000, 999999);
        switch ($sms_type) {
            case 'reg':
                $sendSms['tpl']='set_register';
                $sendSms['data']=array('rand'=>$rand.'','sitename'=>C('qscms_site_name'));
                break;
            case 'gsou_reg':
                $sendSms['tpl']='set_register';
                $sendSms['data']=array('rand'=>$rand.'','sitename'=>C('qscms_site_name'));
                break;
            case 'getpass':
                $sendSms['tpl']='set_retrieve_password';
                $sendSms['data']=array('rand'=>$rand.'','sitename'=>C('qscms_site_name'));
                break;
            case 'login':
                if(!$uid=M('Members')->where(array('mobile'=>$mobile))->getfield('uid')) $this->ajaxReturn(0,'您输入的手机号未注册会员');
                $sendSms['tpl']='set_login';
                $sendSms['data']=array('rand'=>$rand.'','sitename'=>C('qscms_site_name'));
                break;
        }
        $smsVerify = session($sms_type.'_smsVerify');
        if($smsVerify && $smsVerify['mobile']==$mobile && time()<$smsVerify['time']+180) $this->ajaxReturn(0,'180秒内仅能获取一次短信验证码,请稍后重试');
        $sendSms['mobile']=$mobile;


        session($sms_type.'_smsVerify',array('rand'=>substr(md5($rand), 8,16),'time'=>time(),'mobile'=>$mobile));
            $this->ajaxReturn(1,'手机验证码发送成功！');//屏蔽手机验证码




        if(true === $reg = D('Sms')->sendSms('captcha',$sendSms)){
            session($sms_type.'_smsVerify',array('rand'=>substr(md5($rand), 8,16),'time'=>time(),'mobile'=>$mobile));
            $this->ajaxReturn(1,'手机验证码发送成功！');
        }else{
            $this->ajaxReturn(0,$reg);
        }
    }
    /**
     * 检测用户信息是否存在或合法
     */

     public function fogot(){
       
            $this->display();
       
    }


    
    public function ajax_check() {
        $type = I('post.type', 'trim', 'email');
        var_dump($type);
        $param = I('post.param','','trim');
        if(in_array($type,array('username','mobile','email'))){
            $type != 'username' && !fieldRegex($param,$type) && $this->ajaxReturn(0,L($type).'格式错误！');
            $where[$type] = $param;
            $reg = M('Members')->field('uid,status')->where($where)->find();
            if($reg['uid'] && $reg['status'] != 0){
                $this->ajaxReturn(0,L($type).'已经注册');
            }elseif(C('apply.Ucenter') && C('qscms_uc_open')){
                $passport = $this->_user_server();
                $name = 'check_'.$type;
                if(false === $passport->$name($param)){
                    $this->ajaxReturn(0,$passport->get_error());
                }
            }
            $this->ajaxReturn(1);
        }elseif($type == 'companyname'){
            if(C('qscms_company_repeat')==0){
                $reg = M('CompanyProfile')->where(array('companyname'=>$param))->getfield('id');
                $reg ? $this->ajaxReturn(0,'企业名称已经注册') : $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(1);
            }
        }
    }
    
}
?>
